/* 将编辑的数据转为word格式 */
import {
  Document,
  Paragraph,
  Packer,
  TextRun,
  AlignmentType
} from 'docx';

const FONT_SIZE = {
  TITLE_MAIN: 22 * 2,
  DATE: 12 * 2,
  TITLE_PART: 16 * 2,
  P: 11 * 2
};

function space(n) {
  return ' '.repeat(5)
}

export function createWordBuffer(option) {
  let {
    title,
    date,
    sectionTitle,
    content
  } = option

  let contentList = content.map(ct => {
    return ct.map((item, titleIndex, array) => {
      let prefix = ''
      if (array.length > 1) {
        prefix = titleIndex + 1 + '. '
      }
      let title = new Paragraph({
        children: [
          new TextRun({
            text: `${prefix}${item.title}`,
            font: 'Microsoft Yahei',
            size: FONT_SIZE.P
          })
        ]
      })
      return item.list.reduce((prev, curr, index, arr) => {
        let prefix = ''
        if (arr.length > 1) {
          prefix = index + 1 + '. '
        }
        return [...prev, new Paragraph({
          indent: {
            left: 120
          },
          children: [
            new TextRun({
              /* 空格在预览时不会显示，但是转成HTML后，会保留 */
              text: `${space(5)}${prefix}${curr.text}`,
              font: 'Microsoft Yahei',
              size: FONT_SIZE.P
            })
          ]
        })]
      }, [title])
    })
  })

  const doc = new Document({
    sections: [{
      children: [
        // 大标题
        new Paragraph({
          alignment: AlignmentType.CENTER,
          children: [
            new TextRun({
              text: title,
              font: 'Microsoft Yahei',
              size: FONT_SIZE.TITLE_MAIN,
              bold: true
            })
          ]
        }),

        // 日期
        new Paragraph({
          alignment: AlignmentType.CENTER,
          children: [
            new TextRun({
              text: date,
              font: 'Microsoft Yahei',
              size: FONT_SIZE.DATE
            })
          ]
        }),

        // 空行
        // new Paragraph({
        //   children: [
        //     new TextRun({
        //       text: '',
        //       break: 1
        //     })
        //   ]
        // }),

        // 本周工作内容
        new Paragraph({
          children: [
            new TextRun({
              text: sectionTitle[0],
              font: 'Microsoft Yahei',
              size: FONT_SIZE.TITLE_PART,
              bold: true
            })
          ]
        }),
        // 内容
        ...(contentList[0].flat(2)),

        // 下周重点工作
        new Paragraph({
          children: [
            new TextRun({
              text: sectionTitle[1],
              font: 'Microsoft Yahei',
              size: FONT_SIZE.TITLE_PART,
              bold: true
            })
          ]
        }),
        // 内容
        ...(contentList[1].flat(2)),

        // 意见反馈
        new Paragraph({
          children: [
            new TextRun({
              text: sectionTitle[2],
              font: 'Microsoft Yahei',
              size: FONT_SIZE.TITLE_PART,
              bold: true
            })
          ]
        }),
        // 内容
        ...(contentList[2].flat(2)),

        // 培训需求
        new Paragraph({
          children: [
            new TextRun({
              text: sectionTitle[3],
              font: 'Microsoft Yahei',
              size: FONT_SIZE.TITLE_PART,
              bold: true
            })
          ]
        }),
        // 内容
        ...(contentList[3].flat(2)),

        // 待解决问题
        new Paragraph({
          children: [
            new TextRun({
              text: sectionTitle[4],
              font: 'Microsoft Yahei',
              size: FONT_SIZE.TITLE_PART,
              bold: true
            })
          ]
        }),
        // 内容
        ...(contentList[4].flat(2)),

        // 空行
        new Paragraph({
          children: [
            new TextRun({
              text: '',
              break: 1
            })
          ]
        }),

        // 空行
        new Paragraph({
          children: [
            new TextRun({
              text: '',
              break: 1
            })
          ]
        }),
      ]
    }]
  });

  return Packer.toBuffer(doc)
}
